package com.baseproject;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.baseproject.eventbus.EventBus;
import com.baseproject.helper.JumpCallbackHelper;
import com.baseproject.util.LogUtils;
import com.jaeger.library.StatusBarUtil;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by hp on 2017/6/9.
 */

public abstract class BaseFragment extends Fragment{

    protected JumpCallbackHelper mJumpCallbackHelper = new JumpCallbackHelper();

    public void setPurpose(String purpose) {
        mJumpCallbackHelper.setPurpose(purpose);
    }

    public String getPurpose() {
        return mJumpCallbackHelper.getPurpose();
    }

    protected Unbinder unbinder;
    private View statusBarView;
    protected int statusBarColor;

    //获取布局文件ID
    protected abstract int getLayoutId();

    protected abstract void _onCreateView(View view, ViewGroup container, Bundle savedInstanceState);

    public abstract boolean onKeyDown(int keyCode, KeyEvent event);

    protected void initStatusBar() {
        StatusBarUtil.setColor(this.getActivity(), BaseProjectAppCofig.getInstance().getColorPrimary(this.getActivity()), 200);
    }

    protected boolean currentFragmentShowingOnTop() {
        if (getActivity() instanceof  BaseFragmentsActivity) {
            return this.getClass().getSimpleName().equals(((BaseFragmentsActivity)getActivity()).getTopFragmentName())
                    || getActivity().getSupportFragmentManager().getBackStackEntryCount() == 1;
        } else {
            return true;
        }
    }

    @Override
    public void onAttach(Context context) {
        LogUtils.d("[onAttach] this = " + this);
        super.onAttach(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        LogUtils.d("[onCreate] this = " + this);
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LogUtils.d("[onCreateView] this = " + this);
        View view = inflater.inflate(getLayoutId(), container, false);
        unbinder = ButterKnife.bind(this, view);
        _onCreateView(view, container, savedInstanceState);
        return view;
    }

    @Override
    public void onResume() {
        LogUtils.d("[onResume] this = " + this);
        super.onResume();
        if (currentFragmentShowingOnTop()) {
            LogUtils.d("[onResume] really top this = " + this);
            initStatusBar();
        }
    }

    @Override
    public void onPause() {
        LogUtils.d("[onPause] this = " + this);
        super.onPause();
    }

    @Override
    public void onStop() {
        LogUtils.d("[onStop] this = " + this);
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        LogUtils.d("[onDestroyView] this = " + this);
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onDestroy() {
        LogUtils.d("[onDestroy] this = " + this);
        EventBus.getDefault().unRegister(this);
        super.onDestroy();
    }


    @Override
    public void onDetach() {
        LogUtils.d("[onDetach] this = " + this);
        super.onDetach();
    }

    protected void showMessage(String message) {
        Snackbar.make(getView(), message, Snackbar.LENGTH_LONG).show();
    }

}
